#! /usr/bin/env	python
#encoding=utf-8
#生成订单主表数据

import MySQLdb,string,os,sys,random

def random_str(randomlength=8):
    str = ''
    #chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    chars ='abcdefghijklmonpqrstuvwxyz0123456789'
    length = len(chars) - 1
    randoms = random.Random()
    for i in range(randomlength):
        str+=chars[randoms.randint(0, length)]
    return str
try:
	conn = MySQLdb.connect(host ='10.102.107.22',user ='dba',passwd = '1q2w3e4r',db = 'mc_orderdb',charset="utf8")
except MySQLdb.Error, e:
	print "Error %d: %s \n" % (e.args[0], e.args[1])

for i in range(0,10000):
	#生成订单编号格式YYYYMMDDnnnnnnn
	year=random.choice(["2015", "2016", "2014","2016"])
	month=random.choice(["01","02","03","04","05","06","07","08","09","10","11","12"])
	day=random.choice(["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28"])
	seri=random.randint(1,10000000000)
	ordersn="%s%s%s%s"%(year,month,day,str(seri)[-7:])
	#随机获取用户ID
	customerid=random.randint(1,10010)
	#随机生成收货人姓名
	shipping_user1=random.choice(["赵","钱","孙","李","周","吴","郑","王","冯","陈","楮","卫","蒋","沈","韩","杨","朱","秦","尤","许","何","吕","施","张","孔","曹",'严',"华"])
	shipping_user2=random.choice(["平","磊","伟","静","全","雪","雷","昊","天","丽","方","强"])
	shipping_user="%s%s"%(shipping_user1,shipping_user2)
	#随机生成地址
	sqlstr="""
		select beanTwo.region_id as province_id
					,beanTwo.region_name as province
		      ,c.region_id as city_id
		      ,c.region_name as city
					,e.region_id as district_id
		      ,e.region_name as district
		from mc_orderdb.region_info beanOne
		join mc_orderdb.region_info beanTwo on beanTwo.parent_id=beanOne.region_id and beanTwo.region_level=1
		join mc_orderdb.region_info c on c.parent_id=beanTwo.region_id and c.region_level=2
		join mc_orderdb.region_info d on d.parent_id=c.region_id and d.region_level=3
		join mc_orderdb.region_info e on e.parent_id=d.region_id and e.region_level=4
		order by RAND()
		limit 1
	"""
	cursor= conn.cursor(MySQLdb.cursors.DictCursor)
	cursor.execute(sqlstr)
	result = cursor.fetchall()
	for row in result:
		provinceid=row["province_id"]
		cityid=row["city_id"]
		districtid=row["district_id"]
	address=random_str(50)
	#支付方式及金额
	payment_method=random.randint(1,5)
	order_money=0.00
	#生成订单主表记录
	sqlstr="""
	insert into mc_orderdb.order_master(order_sn,customer_id,shipping_user,province,city,district,address,payment_method,order_money)
	values('%s',%s,'%s',%s,%s,%s,'%s',%d,%d)
	"""%(ordersn,customerid,shipping_user,provinceid,cityid,districtid,address,payment_method,order_money)

	cursor.execute(sqlstr)
	cursor.execute('commit;')
	cursor.execute('select LAST_INSERT_ID() as orderid')
	result=cursor.fetchone()
	orderid=result["orderid"]

	#生成订单详情表
	for i in range(0,random.randint(1,5)):
		productid = random.randint(131071,247270)
		product_cnt=random.randint(1,3)
		wid=random.randint(1,3)
		sqlstr="""
			insert into mc_orderdb.order_detail(order_id,product_id,product_name,product_cnt,product_price,w_id)
			values(%d,%d,'%s',%d,0.00,%d)
		"""%(orderid,productid,'',product_cnt,wid)
		cursor.execute(sqlstr)
		cursor.execute('commit')

